package com.yxwl.demo.ui.print;

import androidx.recyclerview.widget.RecyclerView;

import android.view.View;

import com.print.base.bean.PrinterConstantPool;
import com.print.base.bean.TaskCallBean;
import com.print.base.listen.TaskCallback;
import com.print.printer.Command;
import com.yxwl.demo.R;
import com.yxwl.demo.bean.PrinterInfoBean;
import com.yxwl.demo.ui.adapter.PrinterInfoAdapter;
import com.yxwl.demo.ui.base.BaseActivity;
import com.yxwl.demo.utils.OnItemClickListner;
import com.yxwl.demo.ui.print.printer.PrinterUtil;
import com.yxwl.demo.utils.StringUtil;

public class PrinterInfoActivity extends BaseActivity {


    private PrinterInfoAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_printer_info;
    }


    @Override
    protected void initView() {
        RecyclerView recyclerView = findViewById(R.id.list);
        adapter = new PrinterInfoAdapter(this);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListner(new OnItemClickListner() {
            @Override
            public boolean onItemClickListner(View v, int type, int position, Object o) {
                return false;
            }
        });

        findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.setDatas(null);
                getInfo();
            }
        });
    }

    @Override
    protected void initData() {
        getInfo();
    }











    private void addTask(Command command) {
        PrinterUtil.getInstance().printer.addTask(command, "", true, callback);
    }

    private void addTask(String commandType, byte[] content) {
        PrinterUtil.getInstance().printer.addTask(content, commandType, null, true, null);
    }

    TaskCallback callback = new TaskCallback() {

        @Override
        public void readCall(TaskCallBean bean) {
            String title;
            switch (bean.type) {
                case PrinterConstantPool.Command.GET_NAME:
                    title = "设备型号";
                    break;
                case PrinterConstantPool.Command.GET_VERSION:
                    title = "固件版本";
                    break;
                case PrinterConstantPool.Command.GET_BT_NAME:
                    title = "蓝牙名称";
                    break;
                case PrinterConstantPool.Command.GET_BT_VERSION:
                    title = "蓝牙版本";
                    break;
                case PrinterConstantPool.Command.GET_SN:
                    title = "SN";
                    break;
                case PrinterConstantPool.Command.GET_DENSITY:
                    title = "打印浓度";
                    break;
                case PrinterConstantPool.Command.GET_SPEED:
                    title = "打印速度";
                    break;
                case PrinterConstantPool.Command.GET_BATTERVOL:
                    title = "电量";
                    break;
                case PrinterConstantPool.Command.GET_BT_MAC:
                    title = "蓝牙MAC";
                    break;
                case PrinterConstantPool.Command.GET_WIFI_MAC:
                    title = "WiFi MAC";
                    break;
                case PrinterConstantPool.Command.GET_SHUT_TIME:
                    title = "待机时间";
                    break;
                case PrinterConstantPool.Command.GET_WIFI_SSID:
                    title = "WIFI 名称";
                    break;
                case PrinterConstantPool.Command.GET_WIFI_IP:
                    title = "WIFI IP";
                    break;
                default:
                    title = bean.type;
                    break;
            }

            String value = null;
            if (PrinterUtil.getInstance().printer.getDeviceItem().name.startsWith(PrinterConstantPool.Model.CTP800BD)) {
                switch (bean.type) {
                    case PrinterConstantPool.Command.GET_BT_MAC:
                        value = StringUtil.bytetToHex(bean.data);
                        break;
                    case "电压":
                        int i1 = bean.data[2];
                        long i2 = bean.data[3] & 0XFF;
                        value = String.valueOf((i1 * 256 + i2) / 100f);
                        break;
                    case "温度":
                        int wd = bean.data[3];
                        value = String.valueOf(wd);
                        break;
                    case "纸张信息":
                        switch (bean.data[0]) {
                            case 0X00:
                                value = "标签纸";
                                break;
                            case 0X01:
                                value = "黑标纸";
                                break;
                            case 0X02:
                                value = "连续纸";
                                break;
                        }
                        break;
                    case "学纸模式":
                        switch (bean.data[0]) {
                            case 0X00:
                                value = "自动学习";
                                break;
                            case 0X01:
                                value = "手动学习";
                                break;
                        }
                        break;
                    case PrinterConstantPool.Command.GET_BT_VERSION:
                        value = new String(bean.data);
                        break;
                    case PrinterConstantPool.Command.GET_SPEED:
                        int SPEED = bean.data[3];
                        value = String.valueOf((int) (SPEED / 25.4));
                        break;
                }
            }
            if (value == null) {
                value = PrinterUtil.getInstance().printer.data2Str(bean.type, bean.data);
            }
            if (StringUtil.isSpace(title) || StringUtil.isSpace(value)) {
                return;
            }
            adapter.addData(new PrinterInfoBean(title, value));
        }
    };










    private void getInfo() {
        String deviceName = PrinterUtil.getInstance().printer.getDeviceItem().name;
        //AP机器 (RW401AP ITPP941AP等等，944s不包括在内)
        if (deviceName.startsWith(PrinterConstantPool.Model.RW401AP)
                || deviceName.startsWith(PrinterConstantPool.Model.ITPP941AP)
                || deviceName.startsWith(PrinterConstantPool.Model.Y486AP)) {
            APInfo();
        } else if (deviceName.startsWith(PrinterConstantPool.Model.CTP800BD)
                || deviceName.startsWith(PrinterConstantPool.Model.Y468)
        ) {
            CTP800BDInfo();
        } else {
            printerInfo();
        }

    }

    private void printerInfo() {
        addTask(Command.get_deviceName());
        addTask(Command.get_btName());
        addTask(Command.get_version());
        addTask(Command.get_btVersion());

        addTask(Command.get_DeviceInfo());

        addTask(Command.get_DENSITY());
        addTask(Command.get_SPEED());

        addTask(Command.get_shutTime());
        addTask(Command.get_battervol());
        addTask(Command.get_btMAC());
        addTask(Command.get_wifiMAC());
        addTask(Command.get_SN());

        addTask(Command.get_wifiIP());
        addTask(Command.get_wifiSSID());

        addTask(Command.get_PAPER_STATUS());
        addTask(Command.get_LID());
        addTask(Command.get_status());
    }

    private void APInfo() {
        addTask(Command.OPEN_AP());
        addTask(Command.get_deviceName());
        addTask(Command.get_btName());
        addTask(Command.get_version());
        addTask(Command.get_btVersion());

        addTask(Command.get_DeviceInfo());

        addTask(Command.get_DENSITY());
        addTask(Command.get_SPEED());

        addTask(Command.get_shutTime());
        addTask(Command.get_battervol());
        addTask(Command.get_btMAC());
        addTask(Command.get_wifiMAC());
        addTask(Command.get_SN());

        addTask(Command.get_wifiIP());
        addTask(Command.get_wifiSSID());

        addTask(Command.get_PAPER_STATUS());
        addTask(Command.get_LID());
        addTask(Command.get_status());

        addTask(Command.CLOSE_AP());
    }

    private void CTP800BDInfo() {
        addTask(Command.get_deviceName());
        addTask(Command.get_version());
        addTask(Command.get_SN());
        addTask(Command.get_DENSITY());
        addTask(Command.get_SPEED());//111
        addTask(Command.get_btName());
        addTask(Command.get_btMAC());//11
        addTask(Command.get_btVersion());//11
        addTask(Command.get_status());
        addTask("生产日期", test1());
        addTask("温度", test2());
        addTask("里程", test3());
        addTask("电压", test8());

        addTask("纸张信息", test4());
        addTask("支持指令", test5());
        addTask("接口类型", test6());
        addTask("学纸模式", test7());

        adapter.addData(new PrinterInfoBean("蓝牙密码", "0000"));
    }

    /**
     * 生产日期
     */
    public byte[] test1() {
        return new byte[]{0X1F, (byte) 0XB0, 0X08};
    }

    /**
     * 温度
     */
    public byte[] test2() {
        return new byte[]{0X1F, 0X30, 0X21};
    }


    /**
     * 里程
     */
    public byte[] test3() {
        return new byte[]{0X1F, (byte) 0XB0, 0X10};
    }

    /**
     * 纸张信息
     * 返回0是标签纸，1是黑标纸，2是连续纸
     */
    public byte[] test4() {
        return new byte[]{0X1F, (byte) 0X80, 0X00};
    }

    /**
     * 打印机支持的指令，
     * （这个一般用户不会读取，我们机器实际也不止支持TSPL，这里目前就返回TSPL）
     */
    public byte[] test5() {
        return new byte[]{0X1F, (byte) 0XB0, 0X0A};
    }

    /**
     * 打印机支持的接口类型
     * （这个一般用户不会读取，默认返回USB,如果支持蓝牙返回 USB|BT2.0|BT4.0）
     */
    public byte[] test6() {
        return new byte[]{0X1F, (byte) 0XB0, 0X0C};
    }

    /**
     * Locate Model
     * 返回值为0表示自动学习，1表示用户手动学习
     */
    public byte[] test7() {
        return new byte[]{0X1F, (byte) 0X80, 0X04};
    }


    /**
     * 电压
     */
    public byte[] test8() {
        return new byte[]{0X1F, 0X30, 0X20};
    }

    /**
     * 蓝牙密码 为固定值0000四个零
     */
    public byte[] test9() {
        return new byte[0];
    }
}